<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2020/1/8
 * Time: 14:06
 */

namespace app\api\controller;


use app\common\model\FilePath;

class Config extends Base
{
    /**
     * @name 获取syslog配置
     * @author wx
     * @date 2020/1/8 14:26
     */
    public function syslog()
    {
        $server = new \app\api\service\Config();
        $data = $server->syslog();


        $this->jsonOutPut($data);
    }

    public function portConfig()
    {
        $data = base64_decode(file_get_contents("php://input"));

        $server = new \app\api\service\Config();
        $data = $server->portConfig($data);

        $this->jsonOutPut($data);
    }

    /**
     * @name 获取数据库连接配置
     * @author wx
     * @date 2020/1/14 14:34
     */
    public function database()
    {
        $params = base64_decode(file_get_contents("php://input"));

        $config = config('database.');

        $data['ip'] = $config['hostname'];
        $data['port'] = $config['hostport'];
        $data['user'] = $config['username'];
        $data['password'] = $config['password'];
        $data['name'] = $config['database'];

        $this->jsonOutPut($data);
    }


    /**
     * @name 获取服务端相关配置
     * @author wx
     * @date 2020/6/28 15:24
     */
    public function serverConfig()
    {
        $serverConfig['reboot_path'] = $_SERVER['DOCUMENT_ROOT'].'/config/reboot.conf';//重启服务端操作系统路径
        $serverConfig['server_reboot_path'] = $_SERVER['DOCUMENT_ROOT'].'/config/server_reboot.conf';//重启服务端
        $serverConfig['system_config'] = $_SERVER['DOCUMENT_ROOT'].'/config/system_config.conf';//系统配置信息

        $serverConfig['server_pcinfo_path'] = $_SERVER['DOCUMENT_ROOT'].'/config/server_pcinfo.json';//服务器资源路径
        $serverConfig['install_path'] = $_SERVER['DOCUMENT_ROOT'].'/Install/.jzz.lock';//安装时UUID文件路径
        $serverConfig['system_time_json'] = $_SERVER['DOCUMENT_ROOT'].'/Install/system_time.json';//安装时生成的系统时间文件json
        $serverConfig['system_time_lock'] = $_SERVER['DOCUMENT_ROOT'].'/Install/.system_time.lock';//安装时生成的系统时间文件lock(boss create)

//        云端更新地址
        $fileServer = new FilePath();
        $serverConfig['auth_update_online'] = $fileServer->remote_auth_domain.'api/v3/update/online';//云端在线更新地址
        $serverConfig['auth_update_download'] = $fileServer->remote_auth_domain.'api/v3/update/downloadcomplete';//云端下载结果回传
        $serverConfig['auth_update_result'] = $fileServer->remote_auth_domain.'api/v3/update/updateresult';//更新包执行结果回传

        $serverConfig['serial_number'] = db('server_auth')->value('auth_key');//云端授权码

        $serverConfig['public_path'] = $_SERVER['DOCUMENT_ROOT'];//PHP public路径
        $serverConfig['local_update_offline'] = $_SERVER['DOCUMENT_ROOT'].'/static/ServerUpdate/offline/offline.lock';//离线更新标识文件
        $serverConfig['local_update_online'] = $_SERVER['DOCUMENT_ROOT'].'/static/ServerUpdate/online/online.lock';//在线更新标识文件
        $serverConfig['local_updating'] = $_SERVER['DOCUMENT_ROOT'].'/static/ServerUpdate/updating.lock';//正在更新标识文件

        $serverConfig['get_remote_auth'] = 'api/v3/auth/online';//拉取线上授权
        $serverConfig['visit_rate'] = 10 * 60;//访问频率

        $this->jsonOutPut($serverConfig);
    }

    /**
     * @name 重置超级管理员密码
     * @author wx
     * @date 2020/12/2 14:43
     */
    public function modifyPassword()
    {
        $data = file_get_contents("php://input");
        $data = json_decode($data,true);

        if(!isset($data['password']) || strlen($data['password']) != 32){
            $this->jsonErrOut('数据错误', 202);
        }

        $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
        $res = db('server_user')->where('user_group_id', 1)->where('fid',0)->update($data);

        if($res){
            $this->jsonOutPut();
        }else{
            $this->jsonErrOut();
        }


    }
}